package com.datn.DataAccess;

import java.util.ArrayList;
import java.util.List;

import com.datn.activity.ListPlaceActivity;
import com.datn.utils.AlertCustom;
import com.datn.utils.GPSUtils;
import com.datn.utils.NetworkUtils;
import com.datn.utils.PreferenceUtils;
import com.datn.utils.places.ItemCategoryViewGroup;
import com.datn.utils.places.ItemPlaceViewGroup;
import com.datn.utils.places.JsonPlace;
import com.datn.utils.places.Place;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;

import com.datn.activity.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    private LayoutInflater layoutInflater;
    
    public ImageAdapter(Context c) {
        mContext = c;
        layoutInflater = LayoutInflater.from(mContext);
        PreferenceUtils.getInstance(mContext);
    }

    public int getCount() {
        return stCategory.length;
    }

    public Object getItem(int position) {
        return stCategory[position];
    }

    public long getItemId(int position) {
        return position;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(final int position, View convertView, ViewGroup parent) {
    	View workView = convertView;
        
        if (workView == null) {
            workView = new View(mContext);
            workView = layoutInflater.inflate(R.layout.item_category, null);
        }
        
        
        final String stCategorySpecified = this.stCategory[position];
        final int stResourceImage = this.mThumbIds[position];
      
        if (stCategorySpecified!=null){
        	ImageView imgView = (ImageView)workView.findViewById(R.id.imageItemCategory);
        	TextView tvView = (TextView)workView.findViewById(R.id.tvCategoryItemCategory);

        	tvView.setText(stCategorySpecified);
        	imgView.setImageResource(stResourceImage);  	
        	

        	imgView.setOnClickListener(new OnClickListener() {

        		@Override
        		public void onClick(View v) {
        			// TODO Auto-generated method stub
        			GPSUtils gps=new GPSUtils(mContext);
        			Location loc = gps.getCurrentLocation();
        			PreferenceUtils.setResourceCategory(mThumbIds[position]);
        			PreferenceUtils.setCategory(getItem(position).toString());
        			PreferenceUtils.setSearchTypeAnyAddress(false);
        			if (NetworkUtils.isConnected(mContext)){
    					
		        			if (loc!=null){
		        				try {		        					
		        					GeoPoint current = new GeoPoint((int)(loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
		        					PreferenceUtils.getInstance(mContext);
		        					PreferenceUtils.setCurrentLocation(current);
		        					
		        					Intent intent=new Intent(mContext, ListPlaceActivity.class);
		        					
			    					mContext.startActivity(intent);
			    					((Activity)mContext).finish();	        					
		        					
		        				} catch (Exception e) {
		        					// TODO Auto-generated catch block
		        					e.printStackTrace();
		        				}
		        			} else{
		        				Toast.makeText(mContext, "Cannot get your location!\nProgram will get default location.", Toast.LENGTH_LONG).show();
		        				Intent intent=new Intent(mContext, ListPlaceActivity.class);
		        				mContext.startActivity(intent);
		    					((Activity)mContext).finish();
		        			}
        			
        			}
        			else{
						AlertCustom.showAlert(mContext, "Error", "Please turn on network: WIFI, 3G or GPRS...");
					}
        		}
        	});
        
       }
        return workView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            com.datn.activity.R.drawable.atm,com.datn.activity.R.drawable.coffee,
            com.datn.activity.R.drawable.hospital,com.datn.activity.R.drawable.market,com.datn.activity.R.drawable.restaurant,
            com.datn.activity.R.drawable.hotel,com.datn.activity.R.drawable.park,
            com.datn.activity.R.drawable.cinema,com.datn.activity.R.drawable.resort,
    };
    
    private String[] stCategory = {
    		"ATM","Coffee",
    		"Hospital","Market","Restaurant",
    		"Hotel","Park",
    		"Cinema","Resort"
    };

    
    /*public void showListPlace(final String stCategory, final GeoPoint currentLocation){
    	new AsyncTask<String, Void, ArrayList<Place>>() 
		{
			private ProgressDialog mProgressDialog;
			ArrayList<Place> array;
			JsonPlace json_Place;
			@Override
			protected void onPreExecute() 
			{
				array=new ArrayList<Place>();
				json_Place=new JsonPlace(mContext);
				mProgressDialog = new ProgressDialog(mContext);
				mProgressDialog.setMessage("Please Wait...");
				mProgressDialog.setCancelable(false);
				mProgressDialog.setIndeterminate(true);
				mProgressDialog.show();
				Log.v("PreExecute", "OK");
			};

			@Override
			protected ArrayList<Place> doInBackground(String... args) {
				// TODO Auto-generated method stub
				try {
					
					array.addAll((ArrayList<Place>) json_Place.getListAdressByType(stCategory, currentLocation));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				}
				Log.v("doInBackground", "OK");
				return array;
			}

			@Override
			protected void onPostExecute(ArrayList<Place> array) 
			{
				if (mProgressDialog.isShowing()) 
				{
					mProgressDialog.dismiss();
				}
				if (array != null) 
				{
					AlertCustom.showAlert(mContext, "Success", array.get(0).getName());
				} else 
				{
					AlertCustom.showAlert(mContext, "Error", "Cannot receive list place");
				}
					
				Log.v("onPostExecute","OK");
			};
		}.execute("...");
    }*/
	
   
}